//
//  XJYMTTopicsSearchReslultView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/9.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  有名堂帖子搜索结果view

#import "XJYMTTopicsSearchReslultView.h"
//cell
#import "XJYMTTableViewCell.h"



@interface XJYMTTopicsSearchReslultView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

/**搜索结果view*/
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation XJYMTTopicsSearchReslultView
#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[XJYMTTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XJYMTTableViewCell class])];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}




- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark - 二 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupView];
    }
    return self;
}

- (void)setupView {
    self.tableView.frame = self.bounds;
    [self addSubview:self.tableView];
}

#pragma mark - 三 layout

#pragma mark - UITableViewDelegate,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJYMTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XJYMTTableViewCell class])];
    
    
    XJYMTModel *model = self.dataArray[indexPath.row];
    
    [cell configureYMTCellWithAvator:model.avatar time:model.publish_time name:model.publish_name title:model.title content:model.content clickCount:model.click_count];
    
    return cell;
}

#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XJLog(@"点击了第一个cell: %ld--%ld",indexPath.section, indexPath.row);
    
    XJYMTModel *selectedModel = self.dataArray[indexPath.row];
    if (self.xjymtTopicsSearchResultViewClickCellBlock) {
        self.xjymtTopicsSearchResultViewClickCellBlock(selectedModel);
    }
}

- (void)ymtTopicsSearchReslultViewReload {
    [self.tableView reloadData];
}

#pragma mark - 三 点击事件


#pragma mark - 四 网络请求 搜索
- (void)requestSearchTopicsWithTitle:(NSString *)title {
    
    if([NSString isEmptyString:title]) return;
    
    [XJHttpTool requestInvitationYMTSearchTopicsWithTitle:title successH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        
        NSMutableArray *newArr = [NSMutableArray array];
        
        if ([code isEqualToString:@"0"]) {
            NSArray *list = responseObjc[@"data"][@"list"];
            
            for (NSDictionary *tempDic in list)  {
                XJYMTModel *model = [XJYMTModel mj_objectWithKeyValues:tempDic];
                
                [newArr addObject:model];
            }

            self.dataArray = newArr;
            
            dispatch_async(dispatch_get_main_queue(), ^{
        
                [self.tableView reloadData];
            });
        }
    } failH:^(NSError *error) {
        XJLog(@"%@",error);
    }];
}





@end
